package com.gjy.stream.juf;

import org.apache.poi.ss.formula.functions.T;
import org.junit.jupiter.api.Test;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-05-17 09:50:24
 */
public class FunctionTest {

    @Test
    public void test1() {
        fileBigRead();
    }

    public List<T> filter(List<T> list, Predicate<T> predicate) {
        return list.stream().filter(predicate).collect(Collectors.toList());
    }

    public void fileBigRead() {
        File file = new File("D:\\code\\java\\special-topic\\encryption\\pom.xml");
        try (FileInputStream fis = new FileInputStream(file);
             BufferedInputStream bis = new BufferedInputStream(fis)) {
            byte[] bytes = new byte[1024 * 1024];
            StringBuilder sb = new StringBuilder();

            while (bis.read(bytes) != -1) {
                sb.append(new String(bytes, StandardCharsets.UTF_8));
            }

            System.out.println(sb);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void fileRead() {
        File file = new File("D:\\code\\java\\special-topic\\encryption\\file-channel.txt");
        try (FileInputStream fis = new FileInputStream(file)) {
            byte[] bytes = new byte[((int) file.length())];
            fis.read(bytes);
            String s = new String(bytes, StandardCharsets.UTF_8);
            System.out.println(s);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public boolean predicate(String s, Predicate<String> predicate) {
        return predicate.test(s);
    }

    public void consumer(String s, Consumer<String> consumer) {
        consumer.accept(s);
    }

    public String supplier(Supplier<String> supplier) {
        return supplier.get();
    }

    public String function(String s, Function<String, String> function) {
        return function.apply(s);
    }

}
